using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace peonwar
{
	/// <summary>
	/// This is a game component that implements IUpdateable.
	/// </summary>
    public class ParticleComponent : DrawableGameComponent
    {
        private SpriteBatch spriteBatch;
        public List<Emitter> particleEmitterList;
        public static int ParticleCount;

        public ParticleComponent(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            particleEmitterList = new List<Emitter>();
            ParticleCount = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        public void EnableEmitters(Map map)
        {
            if (!SettingManager.CurrentProfil.Particles)
                return;
            if (map.IsEqual(MapManager.Forest))
            {
                this.particleEmitterList[2].Active = true;
                this.particleEmitterList[3].Active = true;
                this.particleEmitterList[4].Active = true;
                this.particleEmitterList[5].Active = false;
                this.particleEmitterList[6].Active = false;
            }

            if (map.IsEqual(MapManager.Desert))
            {
                this.particleEmitterList[2].Active = false;
                this.particleEmitterList[3].Active = false;
                this.particleEmitterList[4].Active = false;
                this.particleEmitterList[5].Active = true;
                this.particleEmitterList[6].Active = false;
            }


            if (map.IsEqual(MapManager.Winter))
            {
                this.particleEmitterList[2].Active = false;
                this.particleEmitterList[3].Active = false;
                this.particleEmitterList[4].Active = false;
                this.particleEmitterList[5].Active = false;
                this.particleEmitterList[6].Active = true;
            }
            this.UnPauseEmitters();
        }
         
        public void DisableEmitters()
        {
            foreach (Emitter emitter in this.particleEmitterList)
            {
                emitter.Active = false;
                emitter.ParticleList.Clear();
            }
        }

        public void PauseEmitters()
        {
            foreach (Emitter emitter in this.particleEmitterList)
            {
                if (emitter.Active)
                emitter.IsDrawing = false;
            }
        }

        public void UnPauseEmitters()
        {
            foreach (Emitter emitter in this.particleEmitterList)
            {
                if (emitter.Active)
                    emitter.IsDrawing = true;
            }
        }

        public void SetBloodEmitterPosBLUE(User player, Camera camera, int index)
        {
            this.particleEmitterList[0].Active = player.IsBleeding;
            if (player.PeonList.Count > index)
            {
                if (player.PeonList[index].DeadTime == 0)
                {
                    this.particleEmitterList[0].Position = new Vector2(player.PeonList[index].Position.X - camera.Position, player.PeonList[index].Position.Y - 15);
                }
                else
                    SetBloodEmitterPosBLUE(player, camera, index + 1);
            }
            else
            {
                player.IsBleeding = false;
                if (index == 0)
                    this.particleEmitterList[0].Position = new Vector2(-50, -50);
            }
        }

        public void SetBloodEmitterPosRED(User player, Camera camera, int index)
        {
            this.particleEmitterList[1].Active = player.IsBleeding;
            if (player.PeonList.Count > index)
            {
                if (player.PeonList[index].DeadTime == 0)
                {
                    this.particleEmitterList[1].Position = new Vector2(player.PeonList[index].Position.X - camera.Position, player.PeonList[index].Position.Y - 15);
                }
                else
                    SetBloodEmitterPosRED(player, camera, index + 1);
            }
            else
            {
                player.IsBleeding = false;
                if (index == 0)
                    this.particleEmitterList[1].Position = new Vector2(-50, -50);
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (Emitter emitter in particleEmitterList)
            {
                emitter.UpdateParticles(gameTime);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            foreach (Emitter emitter in particleEmitterList)
            {
                if (emitter.IsDrawing)
                    emitter.DrawParticles(gameTime, spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
